package cn.bgs.wjx.WjxService.lmpl;


import cn.bgs.ActivityManagement.pojo.Announcement;
import cn.bgs.ActivityManagement.pojo.MaintenanceProjects;
import cn.bgs.CommunityManagement.pojo.Community;
import cn.bgs.UserManagement.pojo.ComplaintsType;
import cn.bgs.UserManagement.pojo.File;
import cn.bgs.UserManagement.pojo.User;
import cn.bgs.utils.R;
import cn.bgs.wjx.WjxMapper.CommunityMapper;
import cn.bgs.wjx.WjxService.CommunityService;
import cn.bgs.wjx.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CommunityServiceImpl implements CommunityService {
    @Autowired
    private CommunityMapper communityMapper;
    //查询小区所有位置
    @Override
    public List<Community> Listcommunity() {
        return communityMapper.Listcommunity();
    }


    //查询用户需要缴费的所有类型
    @Override
    public List<UserVo> ListUser(Integer id) {
        List<UserVo> cc=communityMapper.ListUser(id);
        for (UserVo user : cc) {
            String address=user.getCommunityName()+"小区"+user.getHowMany()+"楼"+user.getHowFloor()+"层"+user.getHowPortal();
            user.setAddress(address);
        }
        ArrayList<UserVo> userVos = removeDuplicateUser(cc);
        return userVos;
    }

    //查询用户如果有多个房子应缴物业费
    @Override
    public List<UserVo> ListUserVo(Integer id,String type) {
        List<UserVo> cc=communityMapper.ListUserVo(id,type);
        for (UserVo user : cc) {
            String address=user.getCommunityName()+"小区"+user.getHowMany()+"楼"+user.getHowFloor()+"层"+user.getHowPortal();
            user.setAddress(address);
        }
        return cc;
    }
    //查询用户缴费的历史记录
    @Override
    public List<TopUpVo> Lifepayhistory(Integer id, String type, String start, String end) {
        return communityMapper.Lifepayhistory(id,type,start,end);
    }

    //查询维修上报信息
    @Override
    public List<MaintenanceVo> Maintenance(Integer id) {
        List<MaintenanceVo> cc=communityMapper.Maintenance(id);
        for (MaintenanceVo user : cc) {
            String address=user.getCommunityName()+"小区"+user.getHowMany()+"楼"+user.getHowFloor()+"层"+user.getHowPortal();
            user.setAddress(address);
        }
        return cc;
    }

    @Override
    public int GwbUpdate(MaintenanceVo maintenanceVo) {
        MaintenanceProjects maintenanceProjects=communityMapper.Projects(maintenanceVo.getTheProjectName());
        int i=maintenanceProjects.getMaintenanceProjectsId();
        maintenanceVo.setProjectTypeId(i);
        int i1=communityMapper.updateMaintenanceVo(maintenanceVo);
        return i1;
    }

    @Override
    public int mainXqxg(Integer maintenanceId) {
        return communityMapper.mainXqxg(maintenanceId);
    }

    // 维修上报的申请添加功能
    @Override
    public List<AddmainListVo> AddmainList(Integer id) {
        List<AddmainListVo> cc=communityMapper.AddmainList(id);
        for (AddmainListVo user : cc) {
            String address=user.getCommunityName()+"小区"+user.getHowMany()+"楼"+user.getHowFloor()+"层"+user.getHowPortal();
            user.setAddress(address);
        }
        return cc;
    }

    @Override
    public int GwbAddTeance(MaintenanceVo maintenanceVo) {
        MaintenanceProjects maintenanceProjects=communityMapper.Projects(maintenanceVo.getTheProjectName());
        int i=maintenanceProjects.getMaintenanceProjectsId();
        if (i!=0){
            maintenanceVo.setProjectTypeId(i);
            int i1=communityMapper.GwbAddTeance(maintenanceVo);
            File file = new File();
            file.setFileId(0);
            file.setFile(maintenanceVo.getUrl());
            file.setAssociatedId(maintenanceVo.getMaintenanceId());
            file.setType("维修申请");
            communityMapper.AddFile(file);

            return i1;
        }else {
            return 0;
        }
    }

    @Override
    public List<GwbActivityVo> GwbActivity(Integer id) {
        return communityMapper.GwbActivity(id);
    }

    @Override
    public List<GwbActivityVo> GwbActivity1(Integer id) {
        return communityMapper.GwbActivity1(id);
    }

    @Override
    public int GwbAddshenqing(GwbshenqingVo gwbshenqingVo) {
        ComplaintsType complaintsType=communityMapper.getType(gwbshenqingVo.getType());
        Integer id=complaintsType.getComplaintsTypeId();
        gwbshenqingVo.setComplaintsTypeId(id);
        System.out.println(gwbshenqingVo);
        int i=communityMapper.GwbAddshenqing(gwbshenqingVo);
        return i;
    }

    @Override
    public List<AnnouncementVo> All(Integer id) {
        return communityMapper.All(id);
    }

    @Override
    public List<AnnouncementVo> All1(Integer id) {
        return communityMapper.All1(id);
    }

    @Override
    public int Zhuce(User user) {
        user.setIdentity("业主");
        int i=communityMapper.Zhuce(user);
        return i;
    }

    @Override
    public int xgqs(UserVo uservo) {
        Integer userid=uservo.getUserId();
        Double balance=uservo.getBalance();
        int i=communityMapper.Xguser(userid,balance);
        if (i==1){
            uservo.setState("2");
            int c=communityMapper.XgtopUp(uservo);
            if (c==1){
                return 1;
            }else {
                return 0;
            }
        }else {
            return 0;
        }
    }


    //去重方法
    private static ArrayList<UserVo> removeDuplicateUser(List<UserVo> users) {
        Set<UserVo> set = new TreeSet<UserVo>(new Comparator<UserVo>() {
            @Override
            public int compare(UserVo o1, UserVo o2) {
                // 字符串,则按照asicc码升序排列
                return o1.getType().compareTo(o2.getType());
            }
        });
        set.addAll(users);
        return new ArrayList<UserVo>(set);
    }



}
